package com.battery.service.impl;

import com.battery.dao.DictDAO;
import com.battery.pojo.dict;
import com.battery.service.DictService;
import com.battery.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DictImpl implements DictService {

    @Autowired(required = false)
    private DictDAO dictdao;

    @Override
    public Result getDict(){
        List<dict> result = new ArrayList<>();
        try {
            result = dictdao.getAllDict();
            if (result!=null){
                return Result.ok().data("allDict",result);
            }
            else {
                return Result.error().message("获取数据出错");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result addDict(dict dic){

        try {
            int result = dictdao.insertDict(dic);
            if (result>0){
                return Result.ok().message("新增字典成功");
            }
            else {
                return Result.error().message("出错");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result deleteDict(int id){

        try {
            int result = dictdao.deleteDict(id);
            if (result>0){
                return Result.ok().message("删除字典成功");
            }
            else {
                return Result.error().message("出错");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

    @Override
    public Result updateDict(dict dic){
        try {
            int result = dictdao.updateDict(dic);
            if (result>0){
                return Result.ok().message("更新字典成功");
            }
            else {
                return Result.error().message("出错");
            }
        } catch (Exception e){
            return Result.error().message("出错");
        }
    }

}
